package com;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TaskFinder implements Runnable{
		private static boolean taskFinderMode=true;	
		private ConcurrentLinkedQueue<Execution> concurrentLinkedQueue; 
		private ExecutorService executorService;		
		
	public TaskFinder(int numberThreads){
		concurrentLinkedQueue= new ConcurrentLinkedQueue<Execution>();
		executorService=Executors.newFixedThreadPool(numberThreads);
	}
					
	public void add(Execution exec){
		concurrentLinkedQueue.add(exec);
	}
	
	public void run(){		 
		while(taskFinderMode){
			while(!concurrentLinkedQueue.isEmpty()){
				executorService.execute(concurrentLinkedQueue.poll());				
			}			
		}
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			System.out.println("Error sleep time");
		}
		executorService.shutdown();
	}
	public static void setTaskFinderMode(boolean _taskFinderMode){
		taskFinderMode=_taskFinderMode;
	}	
}
